<template>
  <div>
    <div>【数据传递信息】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})
const target = ref(null)
let myChart = null
const render = () => {
  const option = {
    xAxis: { show: false },
    yAxis: { show: false },
    series: [
      {
        type: 'graph',
        coordinateSystem: 'cartesian2d',
        symbolSize: 26,
        label: {
          normal: {
            show: true,
            position: 'bottom',
            color: '#5e5e5e'
          }
        },
        edgeLabel: {
          show: true,
          color: '#fff',
          textStyle: {
            fontSize: 14
          },
          formatter: function (params) {
            let txt = ''
            if (params.data.speed !== undefined) {
              txt = params.data.speed
            }
            return txt
          }
        },
        edgeSymbol: ['circle', 'arrow'],
        edgeSymbolSize: [4, 10],
        data: props.data.relations.map((item) => {
          if (item.id !== 0) {
            return {
              name: item.name,
              value: item.value
            }
          } else {
            return {
              name: item.name,
              value: item.value,
              symbolSize: 100,
              // 圆的样式
              itemStyle: {
                normal: {
                  // 渐变色
                  color: {
                    colorStops: [
                      { offset: 0, color: '#157eff' },
                      { offset: 1, color: '#35c2ff' }
                    ]
                  }
                }
              }
            }
          }
        }),
        links: props.data.relations.map((item, index) => ({
          source: item.source,
          target: item.target,
          lineStyle: { color: '#12b5d0', curveness: 0.2 },
          label: {
            show: true,
            position: 'middle',
            offset: [10, 0]
          },
          speed: `${item.speed}kb/s`
        }))
      },
      {
        type: 'lines',
        coordinateSystem: 'cartesian2d',
        z: 1,
        lineStyle: {
          curveness: 0.2
        },
        effect: {
          show: true,
          symbol: 'arrow',
          color: 'rgba(55,155,255,0.5)',
          symbolSize: 12
        },
        data: [
          [{ coord: [0, 300] }, { coord: [50, 200] }],
          [{ coord: [0, 100] }, { coord: [50, 200] }],
          [{ coord: [50, 200] }, { coord: [100, 100] }],
          [{ coord: [50, 200] }, { coord: [100, 300] }]
        ]
      }
    ]
  }
  myChart.setOption(option)
}
onMounted(() => {
  myChart = echarts.init(target.value)
  render()
})
watch(
  () => props.data,
  (newValue) => {
    render()
  }
)
</script>

<style lang="scss" scoped></style>
